<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "Scroll",
    data() {
      return {
        scroll: null
      }
    },
    props:{
      probeType:{     //probeType由父类传递过来,这样节省性能
        type:Number,
        default:0
      },
      pullUpLoad:{
        type:Boolean,
        default: false
      }
    },
    created() {
      this.$nextTick(()=>{
        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType:this.probeType,
          pullUpLoad:this.pullUpLoad,
          click:true
        });
        // this.scroll.on('touchEnd', (pos) => {
        //   this.scroll.refresh()
        // })
        if( this.probeType===2 || this.probeType===3){
          this.scroll.on('scroll', (position) => {
            // console.log(position)
            this.$emit('scrollPostion',position);
          })
        }
        if(this.pullUpLoad){
          this.scroll.on('pullingUp',()=>{
            this.$emit('pullingUp')
            //这里需要结束上拉加载更多
          })
        }
      })
    },
    methods:{
      scrollTo(x,y,time=300){
        this.scroll && this.scroll.scrollTo(x,y,time);
      },
      refresh(){
        this.scroll && this.scroll.refresh();
      },
      finishPullUp(){
        this.scroll && this.scroll.finishPullUp();
      },
      getScrollY(){   //获取Y的位置
        return this.scroll ? this.scroll.y :0;
      }
    }
  }
</script>

<style scoped>

</style>
